package com.scut.online_black_market.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.scut.online_black_market.dto.AlipayOrder;
import com.scut.online_black_market.service.AliPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Optional;

@Api(value = "支付宝接口",tags = "支付宝接口")
@RestController
@RequestMapping(value = "/aliPay")
public class AlipayController {

    @Value("${sdk.alipay.alipay-public-key}")
    private String ALIPAY_PUBLIC_KEY;

    @Resource
    private AliPayService aliPayService;

    @ApiOperation(value = "充值黑币")
    @RequestMapping(value="/Recharge/{money}",method= RequestMethod.GET)
    public ResponseEntity<AlipayOrder> Recharge(@PathVariable float money) {
        try {
            String tradeInfo =  aliPayService.recharge(money);
            return ResponseEntity.ok(new AlipayOrder(tradeInfo));
        } catch (AlipayApiException e) {
            return ResponseEntity.of(Optional.empty());
        }
    }

    /**
     * 支付宝支付成功后.异步请求该接口
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value="/notify",method=RequestMethod.POST)
    public String notify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.从支付宝回调的request域中取值
        //获取支付宝返回的参数集合
        Map<String, String[]> aliParams = request.getParameterMap();
        //用以存放转化后的参数集合
        Map<String, String> conversionParams = new HashMap<>();
        for (Iterator<String> iter = aliParams.keySet().iterator(); iter.hasNext();) {
            String key = iter.next();
            String[] values = aliParams.get(key);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
            }
            // 乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
            // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "uft-8");
            conversionParams.put(key, valueStr);
        }
        String status = aliPayService.validateAliPay(conversionParams);
        return status;
    }

}
